using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class SetMenu : MonoBehaviour
{
    public Slider master, music, sound;
    public Toggle arrow, grid;
    public Image back;
    public Sprite mainMenu, exit;
    private AudioMixer mixer;

    private void OnEnable()
    {
        GameManager.instance.ButtonClick();
        GameManager.instance.isSetting = true;

        mixer = GameManager.instance.audioMixer;
        master.value = GameManager.instance.gameData.audio_master;
        music.value = GameManager.instance.gameData.audio_music;
        sound.value = GameManager.instance.gameData.audio_sound;

        arrow.isOn = (GameManager.instance.gameData.showArrow == 1);
        grid.isOn = (GameManager.instance.gameData.showGrid == 1);

        if (SceneManager.GetActiveScene().name == "MainMenu")
        {
            back.sprite = exit;
        }
        else
        {
            back.sprite = mainMenu;
        }
    }
    private void OnDisable()
    {
        GameManager.instance.isSetting = false;
        GameManager.instance.ButtonClick();
        GameManager.instance.SaveGameData();
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            gameObject.SetActive(false);
        }
    }
    public void SetMaster()
    {
        mixer.SetFloat("MasterValue", master.value * 4);
        GameManager.instance.gameData.audio_master = (int)master.value;
        if (master.value == -10f) mixer.SetFloat("MasterValue", -80f);
        GameManager.instance.ButtonClick();
    }

    public void SetMusic()
    {
        mixer.SetFloat("MusicValue", music.value * 4);
        GameManager.instance.gameData.audio_music = (int)music.value;
        if (music.value == -10f) mixer.SetFloat("MusicValue", -80f);
        GameManager.instance.ButtonClick();
    }

    public void SetSound()
    {
        mixer.SetFloat("SoundValue", sound.value * 4);
        GameManager.instance.gameData.audio_sound = (int)sound.value;
        if (sound.value == -10f) mixer.SetFloat("SoundValue", -80f);
        GameManager.instance.ButtonClick();
    }

    public void ShowArrow()
    {
        GameManager.instance.gameData.showArrow = arrow.isOn ? 1 : 0;
        GameManager.instance.ButtonClick();
    }

    public void ShowGrid()
    {
        GameManager.instance.gameData.showGrid = grid.isOn ? 1 : 0;
        GameManager.instance.ButtonClick();
    }

    public void Back()
    {
        if (SceneManager.GetActiveScene().name == "MainMenu")
        {
            GameManager.instance.ExitGame();
        }
        else
        {
            GameManager.instance.ReturnMainMenu();
        }
    }
}
